<?php

class groups extends a_admin_controller {

    protected $acl_srv;
    protected $user_srv;
    protected $admin_users;
    protected $selected_group_id;
    protected $modules;

    public function __construct($post_handling_enabled = FALSE) {
        parent::__construct($post_handling_enabled);
        include_once get_module_path("acl", module_u_acl::ACL_PERMISSION_DRIVERS_FOLDER)."driver_module_permission".EXT;
        $this->run_auto_view = false;
        $this->acl_srv = new service_l_acl();
        $this->user_srv = new user_service_l_user();
        $this->selected_group_id = $this->input->post("select_group");
        $this->modules  = driver_module_permission::get_modules($this->selected_group_id ? $this->selected_group_id : NULL);
    }

    public function index() {
        //$this->manage_groups();
        $this->view();
    }

//    public function groups_n_users() {
//
//        $this->set_ui_tab_index(2);
//        $this->view("groups");
//    }
//
//    public function manage_groups() {
//        $this->set_ui_tab_index(1);
//        $this->view("groups");
//    }
//
//    public function manage_module_acl() {
//        $this->set_ui_tab_index(3);
//        $this->view("groups");
//    }


    public function batch_assign() {
        $this->assign("groups", $this->acl_srv->list_all_groups(TRUE));
        $this->assign("admin_users", $this->user_srv->get_system_users());
        $this->assign("modules", $this->modules);
        $this->assign("selected_group_id",$this->selected_group_id);
        
    }

    public function create_group() {
        $name = $this->input->post("group_name");
        $desc = $this->input->post("group_desc");
        if ($name) {
            $id = $this->acl_srv->create_group($name, $desc);
            return $id !== NULL ? $id : "nok";
        } else {
            return "nok";
        }
    }

    public function delete_group() {
        $id = $this->input->post('group_id');
        if ($id && is_numeric($id) && $id !== "1") {
            return $this->acl_srv->delete_group($id) !== NULL ? $id : "nok";
        } else {
            return "nok";
        }
    }

    public function assign_group() {
        $user_id = $this->input->post("user_id");
        $group_id = $this->input->post("group_id");
        if ($user_id && $group_id) {
            $obj = new group_user_m_acl();
            $obj->where("user_id", $user_id)->get();
            if ($obj->exists()) {
                $obj->delete();
            }            
            $obj->user_id = $user_id;
            $obj->acl_group_id = $group_id;
            $obj->save_as_new();
            return "ok";
        }else{
            return "nok";
        }
    }

    public function toggle_module_permission(){
        $module_name = $this->input->post("module_name");
        $group_id    = $this->input->post("group_id");
        $flag        = $this->input->post("flag");
        if($module_name && $group_id){
            $falg = $flag=="1" ? TRUE:FALSE;
            driver_module_permission::enlable_diable_permission($module_name, $group_id, $flag);
            return "ok";
        }else  return "nok";
    }
    public function __destruct() {
        unset($this->acl_srv);
        unset($this->user_srv);
    }

}
?>